<?php
	function db_connection(){
		global $CONFIG;
		$host		= $CONFIG["db_host"];
		$user		= $CONFIG["db_user"];
		$password	= $CONFIG["db_pass"];
		$database	= $CONFIG["db_name"];
		$db	= mysql_connect($host,$user,$password) or die("数据库连接中......");//建立数据库链接
		$select_db	= mysql_select_db($database,$db) or die("数据库选择中......");//选择某个数据库
		mysql_query("set names utf8;");
	}
	// 从数据表添加数据
	function db_add($table,$dataA) {
		if($table && count($dataA)>0) {
			$strleft='';
			$strright='';
			foreach($dataA as $key=>$val) {
				$strleft.=','.$key;
				$strright.=','.$val;
			}
			$strleft='insert into '.$table.' ('.ltrim($strleft,',').')';
			$strright=' values ('.ltrim($strright,',').')';
			$sql=$strleft.$strright;
			db_query($sql);
			return db_insert_id();
		}
	}

	// 修改数据表信息
	function db_mdf($table,$dataA,$id) {
		if($table && count($dataA)>0 && $id) {
			$setsql='';
			$wheresql='';
			foreach($dataA as $key=>$val) {
				$setsql.=', '.$key.'='.$val;
			}
			$setsql = ltrim($setsql,',');
			$wheresql = " id in(". $id .")";
			$sql='update '.$table.' set '.$setsql;
			$sql.=' where '.$wheresql;
			db_query($sql);
		}
	}
	
	// 返回数据表$table中id=$id的当前行的数据
	function db_get_val($table,$id,$field) {
		$result=db_query("select $field from $table where id=$id");
		// mysql_fetch_array获取当前行
		$rs = mysql_fetch_array($result);
		return $rs[$field];
	}
	
	// 删除 $table中$id的记录
	function db_del($table,$id) {
		if($table && $id) {
			$wheresql=' id in('. $id .')';
			$sql="delete from `".$table."` where ".$wheresql;
			db_query($sql);
		}
	}
	
	// 删除表中条件与$where一致的记录
	function db_dela($table,$where) {
		if($table && $where) {
			$sql="delete from `".$table."` where ".$where;
			db_query($sql);
		}
	}
	
	// 取出当前行
	function db_get_row($sql) {
		$result=db_query($sql);
		$rs = mysql_fetch_array($result);
		return $rs;
	}
	
	// 取出所有
	function db_get_all($sql) {
		$result=db_query($sql);
		$rs = array();
		while( $row = mysql_fetch_array($result)){
			$rs[] = $row;
		}
		return $rs;
	}

	function db_get_page($sql,$page,$page_size) {
		$page = $page*1?$page:1;
		$sumss = db_query($sql);
		//总数
		$total = db_num_rows($sumss);
		if (ceil($total/$page_size)<$page){
			$page = ceil($total/$page_size);
		}
		$start = ($page-1)*$page_size;
		$rs_sql = $sql." limit $start,$page_size";
		$rsData = db_get_all($rs_sql);
		$pageA = array();
		$pageA["page"] = $page;
		$pageA["page_size"] = $page_size;
		$pageA["total"] = $total;
		$pageA["data"] = $rsData;
		return $pageA;
	}
	
	// 审核商品
	function db_cstatus($table,$sel_id,$sel_do) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				$sql="update `".$table."` set status='".$sel_do."' where ".$wheresql;
				db_query($sql);
			}
		}
	}
	function db_seldelup($table,$sel_id,$sel_do) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				$sql="update `".$table."` set isno=1 where ".$wheresql;
				db_query($sql);
			}
		}
	}
	// 商品上架
	function db_isno0($table,$sel_id,$sel_do) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				$sql="update `".$table."` set isno=0 where ".$wheresql;
				db_query($sql);
			}
		}
	}
	
	// 管理员禁止用户
	function db_jin($table,$sel_id,$sel_do) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				$sql="update `".$table."` set status=1 where ".$wheresql;
				db_query($sql);
			}
		}
	}
	
	// 管理员恢复用户
	function db_hui($table,$sel_id,$sel_do) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				$sql="update `".$table."` set status=0 where ".$wheresql;
				db_query($sql);
			}
		}
	}
	
	// 删除选中的记录
	function db_seldel($table,$sel_id) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				$sql="delete from `".$table."` where ".$wheresql;
				db_query($sql);
			}
		}
	}
	function delbbs($table,$sel_id) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				db_dela($table,"reid=".$sel_id[$i]);
				$sql="delete from `".$table."` where ".$wheresql;
				db_query($sql);
			}
		}
	}
	function delbbs1($table,$sel_id) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				db_dela($table,"startid=".$sel_id[$i]);
				$sql="delete from `".$table."` where ".$wheresql;
				db_query($sql);
			}
		}
	}
	
	// 设置推荐商品
	function isnice1($table,$sel_id,$sel_do) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				$sql="update `".$table."` set isnice=1 where ".$wheresql;
				db_query($sql);
			}
		}
	}
	
	// 取消推荐
	function isnice0($table,$sel_id,$sel_do) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				$sql="update `".$table."` set isnice=0 where ".$wheresql;
				db_query($sql);
			}
		}
	}
	
	function delcategory($table,$id) {
		if($table && $id) {
			$wheresql=' id in('. $id .')';
			db_dela("category","pid=".$id);
			db_dela("goods","categoryid=".$id);
			db_dela("ordersta","categoryid=".$id);
			db_dela("orders","categoryid=".$id);
			db_dela("user","categoryid=".$id);
			db_dela("goods","categoryid=".$id);
			$sql="delete from `".$table."` where ".$wheresql;
			db_query($sql);
		}
	}

	// 删除商品
	function delgoods($table,$sel_id) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				db_dela("orders","goodsid=".$sel_id[$i]);
				db_dela("ordersta","goodsid=".$sel_id[$i]);
				db_dela("collect","goodsid=".$sel_id[$i]);
				$sql="delete from `".$table."` where ".$wheresql;
				db_query($sql);
			}
		}
	}
	// 删除用户
	function deluser($table,$sel_id) {
		if($table && $sel_id[0]) {
			for ($i=0; $i<count($sel_id); $i++){
				$wheresql=' id in('. $sel_id[$i] .')';
				db_dela("news","userid=".$sel_id[$i]);
				db_dela("goods","userid=".$sel_id[$i]);
				db_dela("orders","userid=".$sel_id[$i]);
				db_dela("ordersta","userid=".$sel_id[$i]);
				db_dela("collect","userid=".$sel_id[$i]);
				$sql="delete from `".$table."` where ".$wheresql;
				db_query($sql);
			}
		}
	}
	
	function db_query($sql,$dbconn='')
	{
		Return mysql_query($sql);
	}
	
	// 返回数据的行数
	function db_num_rows($result)
	{
		Return mysql_num_rows($result);
	}

	function db_insert_id()
	{
		Return mysql_insert_id();
	}
?>